package com.wythe.mall.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wythe.mall.R;
import com.wythe.mall.model.GlideLoader;
import com.yancy.imageselector.ImageConfig;
import com.yancy.imageselector.ImageSelector;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Andrew on 2016/12/29.
 * 发布图文 图片列表适配器
 */
public class RecommentImgAdapter extends BaseAdapter {

    private final int MAX_COUNT = 3;
    private ArrayList<String> mImages;
    private LayoutInflater mLayoutInflater;

    private Context mContext;
    private OnImagesChangedCallback mCallBack;

    public RecommentImgAdapter(Context context, OnImagesChangedCallback callback) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        mCallBack = callback;
    }

    public void setImages(List<String> list) {
        if (mImages == null) {
            mImages = new ArrayList<>();
        }
        mImages.clear();
        if (list != null && list.size() > 0) {
            mImages.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        int imageCount = mImages != null ? mImages.size() : 0;
        return imageCount;
    }

    @Override
    public String getItem(int position) {
        if (position >= 0 && mImages != null && position < mImages.size()) {
            return mImages.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_recomment_image, parent, false);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String url = getItem(position);
        Glide.with(mContext)
                .load(url)
                .error(R.drawable.notice__add_to)
                .placeholder(R.drawable.notice__add_to)
                .dontAnimate()
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.ivPreview);
        return convertView;
    }

    class ViewHolder {
        ImageView ivPreview;

        ViewHolder(View itemView) {
            ivPreview = itemView.findViewById(R.id.iv_preview);
            itemView.setTag(this);
        }

    }

    /**
     * 图片变化回调
     */
    public interface OnImagesChangedCallback {
        /**
         * 图片更改
         */
        void onImagesChanged();
    }
}
